package com.itic.mall.system.baokuan.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.DateHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.system.product.persistence.model.Product;
import com.itic.mall.system.product.persistence.model.ProductExample;
import com.itic.mall.system.product.persistence.model.ProductExample.Criteria;
import com.itic.mall.system.product.service.ProductService;
import com.itic.mall.system.productproperty.persistence.model.ProductProperty;
import com.itic.mall.system.productproperty.persistence.model.ProductPropertyExample;
import com.itic.mall.system.productproperty.service.ProductPropertyService;
import com.itic.mall.system.productpropertyvalue.persistence.model.ProductPropertyValue;
import com.itic.mall.system.productpropertyvalue.persistence.model.ProductPropertyValueExample;
import com.itic.mall.system.productpropertyvalue.service.ProductPropertyValueService;
import com.itic.mall.system.productsku.persistence.model.ProductSku;
import com.itic.mall.system.productsku.persistence.model.ProductSkuExample;
import com.itic.mall.system.productsku.service.ProductSkuService;


@Controller
@RequestMapping(value="/system/baokuan")
public class BaokuanController
     extends BaseController {
	
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductPropertyService productPropertyService;
    
    @Autowired
    private ProductSkuService productSkuService;
    
    @Autowired
    private ProductPropertyValueService productPropertyValueService;

    

    @Autowired
   	protected OutCommonService outCommonService;
       
       //////////////////////////////////////////////////////常量区////////////////////////////////////////////////////////////////
       private static final String MODEL_NAME_CHINA = "商品爆款";//中文名
       private static final String MODEL_NAME＿ENGLISH = "baokuan";//英文名
       private static final String PAGE_COMMON_PATH = "/mall/system/baokuan/baokuan";//通用路径
       private static final String PARENT_COMMON_URL = "/system/category/";//通用路径
       private static final String COMMON_URL = "/system/baokuan/";//通用路径
       
       //////////////////////////////////////////////////////页面跳转区////////////////////////////////////////////////////////////
      
       /*
        * 主页面
        */
       @RequestMapping(value="/init")
       public String init(Model model){
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"Main";
       }
       
       /*
        * 主页面
        */
       @RequestMapping(value="/initList")
       public String initList(String categoryId,Model model){
       	model.addAttribute("categoryId", categoryId);
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"List";
       }
       
       /*
        * 添加页面
        */
       @RequestMapping(value="/initAdd")
       public String initAdd(String categoryId,Model model){
       	if( StringHelper.isNotBlank(categoryId) ){
       		model.addAttribute("categoryId", categoryId);
       	}
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"Add";
       }
       
       /*
        * 编辑页面
        */
       @RequestMapping(value="/initEdit")
       public String initEdit(String id , String categoryId, Model model){
       	if( StringHelper.isNotBlank(id) ) {
       		Product temp = productService.get(id);
       		model.addAttribute("editData",iticJsonMapper.toJson(temp));
       		model.addAttribute("name_china", MODEL_NAME_CHINA);
           	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
           	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
           	model.addAttribute("common_url", COMMON_URL);
           	model.addAttribute("categoryId", categoryId);
       	}else {
               throw new ParameterErrorException();
           }
       	return PAGE_COMMON_PATH+"Edit";
       }
       
       
       /*
        * 查看页面
        */
       @RequestMapping(value="/initView")
       public String initView(String id , String categoryId, Model model){
       	if( StringHelper.isNotBlank(id) ) {
       		
       		Product temp  = productService.get(id);
    		
       		model.addAttribute("product",iticJsonMapper.toJson(temp));
       		
       		ProductSkuExample example = new ProductSkuExample();
       		example.setOrderByClause("IS_MAIN desc");
       		com.itic.mall.system.productsku.persistence.model.ProductSkuExample.Criteria criteria = example.createCriteria();
       		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
       		criteria.andProductIdEqualTo(id);
       		List<ProductSku> list = productSkuService.list(example);
       		model.addAttribute("productSkus",iticJsonMapper.toJson(list));
       		
       		model.addAttribute("name_china", MODEL_NAME_CHINA);
           	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
           	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
           	model.addAttribute("common_url", COMMON_URL);
           	model.addAttribute("categoryId", categoryId);
           	
       	}else {
               throw new ParameterErrorException();
           }
       	
       	return PAGE_COMMON_PATH+"View";
       }
       
       /*
        * 导入页面
        */
       @RequestMapping(value="/initImp")
       public String initImp(Model model){
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"Imp";
       }
       
       
       //////////////////////////////////////////////////////业务代码区////////////////////////////////////////////////////////////
       
       /**
        * 添加模板管理数据
        * @param android
        * @return
        */
       @RequestMapping(value="/add")
       @ResponseBody
       public Map<String,String> add( String id, Integer baokuanOrder ){
       	
       	    Map<String, String> resultMap = new HashMap<String, String>();
            String result = ResponseCode.SERVER_ERROR.getCode();
            
            //判断是添加新的商品还是  只添加一个SKU商品
            if(  StringHelper.isNotBlank( id ) ){
            	
            	Product product = productService.get(id);
            	product.setIsBaokuan("001");
            	product.setBaokuanOrder(baokuanOrder);
            	result = productService.edit(product);
            	
            }
            resultMap.put(IticConst.RESULT, result);
            return resultMap;
       }
       
       
       /**
        * <p>Title:queryCondition </p>
        * <p>Description:封装查询条件 </p>
        * @param jzdw
        * @param jzdwExample
        * @return
        */
       private ProductExample queryCondition(HttpServletRequest request,Product temp) {
       	
       	   // 参数处理
           ProductExample example = new ProductExample();
           // 按创建时间倒序
           example.setOrderByClause("CREATE_TIME DESC");
           // 按排列号顺序
           example.setOrderByClause("BAOKUAN_ORDER ASC");
           
           Criteria criteria = example.createCriteria().andIsEffectiveEqualTo( DBConst.TRUE ).andIsBaokuanEqualTo("001");
           
           if( StringHelper.isNotBlank(temp.getCategoryId()) && !"0".equals(temp.getCategoryId())){
        	   //过虑类别条件
               criteria.andCategoryIdEqualTo(temp.getCategoryId());
           }
           
           // 查询条件
           if ( null != temp ) {
       	       //模糊查询
               if ( StringHelper.isNotBlank( temp.getName() ) ) {
                   criteria.andNameLike("%" + temp.getName() + "%");
               }
               
           }
           return example;
       }
       
       /**
        * 
        * @param goldType
        * @param request
        * @return
        */
       @RequestMapping(value = "/list")
       @ResponseBody
       public Map<String, Object> list(Product temp, HttpServletRequest request){
           // 声明返回对象
           Map<String, Object> resultMap = new HashMap<String, Object>();
           
           
           ProductExample example = this.queryCondition(request,temp);
           // 封装分页参数
           example = this.handlePageQuery(request, example);
           
           
           // 获取信息总条数
           int total = productService.count(example);
           
           // 获取数据
           List<Product> list = productService.list(example);
           
           // 数据返回处理
           resultMap.put(IticConst.TOTAL_RECORDS, total);
           resultMap.put(IticConst.DISPLAY_RECORDS, total);
           resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
           resultMap.put(IticConst.DATA, list);
           return resultMap;

       }
       
       /**
        * 编辑保存
        * @param Category
        * @return
        */
       @RequestMapping(value="/edit")
       @ResponseBody
       public Map<String,String> edit( Product temp ){
       
       	    Map<String, String> resultMap = new HashMap<String, String>();
            String result = ResponseCode.SERVER_ERROR.getCode();
            result = productService.edit(temp);
            
            resultMap.put(IticConst.RESULT, result);
            return resultMap;
       }
       
       

       /**
        * 删除
        * 
        */
       @RequestMapping(value = "/del")
       @ResponseBody
       public Map<String, String> del(String id) {
           Map<String, String> resultMap = new HashMap<String, String>();
           String result = ResponseCode.SERVER_ERROR.getCode();

           Product product = new Product();
           product.setId(id);
           product.setIsBaokuan("002");
           
           //当前商品的数量减一
           result = productService.edit(product);
           
           resultMap.put(IticConst.RESULT, result);
           return resultMap;
       }
       
       /**
        * 删除
        * 
        */
       @RequestMapping(value = "hongbao")
       @ResponseBody
       public Map<String, Object> hongbao( String id, String hongbao, String startTime,String stopTime ) {
           Map<String, Object> resultMap = new HashMap<String, Object>();
           String result = ResponseCode.SERVER_ERROR.getCode();

           //删除一个
           //当前商品的数量减一
           Product product = productService.get(id);
          
           if(  DateHelper.isGreater(DateHelper.formatDateTime(new Date()), product.getStopTime()+" 23:59:59") ){
        	   product.setStartTime(startTime);
               product.setStopTime(stopTime);
               product.setProductHongbao(hongbao);
               result = productService.edit(product);
               resultMap.put("data", "success");
           }else{
        	   result = "sucess";
        	   resultMap.put("data", "fail");
        	   resultMap.put("msg", "红包未到期，暂时不能设置！");
           }
           resultMap.put(IticConst.RESULT, result);
           return resultMap;
       } 
       
       /**
        * 获取所有的商品属性
        * 
        */
       @RequestMapping(value = "/getProductPropertys")
       @ResponseBody
       public List<ProductProperty> getProductPropertys() {
          ProductPropertyExample example = new ProductPropertyExample();
          example.setOrderByClause("reorder asc");
          com.itic.mall.system.productproperty.persistence.model.ProductPropertyExample.Criteria criteria = example.createCriteria();
          criteria.andIsEffectiveEqualTo(DBConst.TRUE);
          List<ProductProperty> list = productPropertyService.list(example);
          return list;
       }
       
       
       /**
        * 根据商品的一个属性获得所有的属性值
        * 
        */
       @RequestMapping(value = "/getProductPropertyValues")
       @ResponseBody
       public List<ProductPropertyValue> getProductPropertyValues(String propertyId) {
    	   ProductPropertyValueExample example = new ProductPropertyValueExample();
    	   example.setOrderByClause("reorder asc");
    	   com.itic.mall.system.productpropertyvalue.persistence.model.ProductPropertyValueExample.Criteria criteria = example.createCriteria();
    	   criteria.andIsEffectiveEqualTo(DBConst.TRUE).andPropertyIdEqualTo(propertyId);
    	   List<ProductPropertyValue> list = productPropertyValueService.list(example);
    	   return list;
           
       }
       
       /**
        * 根据ID获取所有的商品属性名称
        * 
        */
       @RequestMapping(value = "/getProductPropertyName")
       @ResponseBody
       public ProductProperty getProductPropertyName( String id ) {
          ProductProperty productProperty = productPropertyService.get(id);
          return productProperty;
       }
       
       /**
        * 根据ID获取所有的商品属性值名称
        * 
        */
       @RequestMapping(value = "/getProductPropertyValueName")
       @ResponseBody
       public ProductPropertyValue getProductPropertyValueName( String id ) {
          ProductPropertyValue productPropertyValue = productPropertyValueService.get(id);
          return productPropertyValue;
       }
       
       
       /**
        * 根据ID获取所有的商品属性值名称
        * 
        */
       @RequestMapping(value = "/products")
       @ResponseBody
       public List<Product> products( String categoryId ) {
    	   ProductExample example = new ProductExample();
    	   example.setOrderByClause("CREATE_TIME DESC");
    	   example.setOrderByClause("REORDER ASC");
    	   Criteria criteria = example.createCriteria();
    	   criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	   if( StringHelper.isNotBlank(categoryId) ){
    		   criteria.andCategoryIdEqualTo(categoryId);
    	   }
          return productService.list(example);
       }
       
       
}